<?php
//abstract class State{
//    public abstract function  handle(Context $context);
//}
//
//class ConcreteStateA extends State{
//    public function handle(Context $context){
//        $context->state = new ConcreteStateB();
//    }
//}
//
//class ConcreteStateB extends State{
//    public function handle(Context $context)
//    {
//        $context->state = new ConcreteDecoratorA();
//    }
//}
//
//class Context{
//    private $state;
//
//    public function __construct($state)
//    {
//        $this->state = $state;
//    }
//
//    public function request(){
//        $this->state->handle(this);
//    }
//}
//
////客户端调用
//$context = new Context(new ConcreteDecoratorA());
//$context->request();
//$context->request();
//$context->request();
//$context->request();

//工作例子
abstract class State{
    public abstract function writeProgram(Work $w);
}

class ForenoonState extends State{
    public function writeProgram(Work $w){
        if($w->hour <12){
            echo '工作状态很好';
        }else{
            $w->setState(new NoonState());
            $w->writeProgram();
        }
    }
}

class NoonState extends State{
    public function writeProgram(Work $w){
        if($w->hour <13){
            echo '边吃边工作';
        }else{
            $w->setState(new AfternoonState());
            $w->writeProgram();
        }
    }
}

class AfternoonState extends State{
    public function writeProgram(Work $w){
        if($w->hour <17){
            echo '迷迷糊糊';
        }else{
            $w->setState(new EveningState());
            $w->writeProgram();
        }
    }
}

class EveningState extends State{
    public function writeProgram(Work $w){
        if($w->hour <21){
            echo '加班，疲劳至极';
        }else{
            $w->setState(new EveningState());
            $w->writeProgram();
        }
    }
}

class SleepState extends State{
    public function writeProgram(Work $w){
        echo '睡着了';
    }
}

class Work{
    private $state;
    public  $hour;
    public $finish;
    public function __construct()
    {
        $this->state = new ForenoonState();
    }

    public function setState($state)
    {
        $this->state = $state;
    }

    public function writeProgram(){
        $this->state->writeProgram($this);
    }
}

//客户端调用
//$work = new Work();
//$work->writeProgram();
//$work->hour = 17;
//$work->writeProgram();
//$work->hour = 9;
//$work->writeProgram();
//$work->hour = 13;
//$work->writeProgram();

/*$div=10;
$total=100;
$avg = $total/$div;
$abc = $total*0.3;
$a = [];

for($i=0;$i<$div-1;$i++){
    $max = $total<($avg+$abc)?$total:$avg+$abc;
    $b = rand(0,$max);
    $total = $total-$b;
    $a[] = $b;
}
var_dump($a);
$a[] = $total;
echo array_sum($a);*/


/*function randomDivInt($div,$total){
    $remain=$total;
    $max_sum=($div-1)*$div/2;
    $p=$div; $min=0;
    $a=array();
    for($i=0; $i<$div-1; $i++){
        $max=($remain-$max_sum)/($div-$i);
        $e=rand($min,$max);
        $min=$e+1; $max_sum-=--$p;
        $remain-=$e;
        $a[$e]=true;
    }
    $a=array_keys($a);
    $a[]=$remain;
    return $a;
}


for($i=0; $i<3; $i++){
    $a=randomDivInt(50,100000);
    var_dump($a);//$a中便是分的不等数
    var_dump(array_sum($a));
}*/


/*$total = 100; //待划分的数字
$div = 5; //分成的份数
$area = 50; //各份数间允许的最大差值

$average = round($total / $div);//平均值
$sum = 0;
$result = array_fill( 1, $div, 0 );
//var_dump($result);
//exit;

for( $i = 1; $i < $div; $i++ ){
    //根据已产生的随机数情况，调整新随机数范围，以保证各份间差值在指定范围内
    if( $sum > 0 ){
        $max = 0;
        $min = 0 - round( $area / 2 );
    }elseif( $sum < 0 ){
        $min = 0;
        $max = round( $area / 2 );
    }else{
        $max = round( $area / 2 );
        $min = 0 - round( $area / 2 );
    }

    //产生各份的份额
    $random = rand( $min, $max );
    $sum += $random;
    $result[$i] = $average + $random;
}

//最后一份的份额由前面的结果决定，以保证各份的总和为指定值
$result[$div] = $average - $sum;

//结果呈现
echo '划分情况：';
foreach( $result as $temp ){
    echo $temp, '---';
}
echo '总和：', array_sum( $result );
exit;*/
$BeginDate = '2017-11-01';
echo strtotime('2017-11');
echo date('Y-m-d H:i:s',strtotime("$BeginDate +1 month -1 second"));